package com.jrauto.CarAppBackend.service.impl;

/**
 * @author : huiMing
 * Date : 2025年09月11日 17:00
 * @version V1.0
 */

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jrauto.CarAppBackend.entity.Message;
import com.jrauto.CarAppBackend.entity.dto.MessageDTO;
import com.jrauto.CarAppBackend.mapper.MessageMapper;
import com.jrauto.CarAppBackend.service.ChatRoomService;
import com.jrauto.CarAppBackend.service.MessageService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;



/**
 * 消息服务实现类
 */
/**
 * 消息服务实现类
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private ChatRoomService chatRoomService;

    @Override
    public MessageDTO sendMessage(Integer roomId, Integer senderId, Integer receiverId,
                                  String messageType, String content) {
        // 创建消息实体
        Message message = new Message();
        message.setRoomId(roomId);
        message.setSenderId(senderId);
        message.setReceiverId(receiverId);
        message.setType(messageType);
        message.setContent(content);
        message.setIsRead(0); // 0-未读
        message.setCreatedAt(LocalDateTime.now());
        message.setUpdatedAt(LocalDateTime.now());

        // 保存消息
        save(message);

        // 更新聊天室最后消息信息
        String lastContent = content;
        if ("IMAGE".equals(messageType)) {
            lastContent = "[图片]";
        } else if ("VOICE".equals(messageType)) {
            lastContent = "[语音]";
        }
        chatRoomService.updateLastMessage(roomId, lastContent);

        // 增加接收者的未读消息数
        chatRoomService.increaseUnreadCount(roomId, receiverId);

        // 转换为DTO返回
        MessageDTO messageDTO = new MessageDTO();
        BeanUtils.copyProperties(message, messageDTO);

        return messageDTO;
    }

    @Override
    public IPage<MessageDTO> getRoomMessages(Integer roomId, Integer page, Integer size) {
        Page<MessageDTO> pageParam = new Page<>(page, size);
        return messageMapper.selectRoomMessages(pageParam, roomId);
    }

    @Override
    public List<MessageDTO> getUnreadMessages(Integer userId) {
        return messageMapper.selectUnreadMessages(userId);
    }

    @Override
    public boolean markMessagesAsRead(Integer roomId, Integer userId) {
        // 批量标记消息为已读
        int result = messageMapper.batchMarkAsRead(roomId, userId);

        // 清零未读消息数
        if (result > 0) {
            chatRoomService.clearUnreadCount(roomId, userId);
        }

        return result > 0;
    }

    @Override
    public Integer countUnreadMessages(Integer roomId, Integer userId) {
        return messageMapper.countUnreadMessages(roomId, userId);
    }

    @Override
    public MessageDTO getLatestMessage(Integer roomId) {
        return messageMapper.selectLatestMessage(roomId);
    }

    @Override
    public List<Integer> getRoomUserIds(Integer roomId) {
        return messageMapper.selectRoomUserIds(roomId);
    }

    @Override
    public List<Message> getOfflineMessages(Integer userId) {
        return messageMapper.selectOfflineMessages(userId);
    }

    @Override
    public boolean markMessagesAsRead(List<Message> messages) {
        if (messages == null || messages.isEmpty()) {
            return true;
        }

        List<Integer> messageIds = messages.stream()
                .map(Message::getId)
                .collect(java.util.stream.Collectors.toList());

        return messageMapper.batchMarkMessagesAsRead(messageIds) > 0;
    }
}
